#-*-coding:utf-8-*-
# 时间:2019/8/29
# 作者:zfh
import logging
import readConfig
import os
from datetime import datetime
import threading

proDir = readConfig.proDir


class Log(object):
    def __init__(self):
        global logPath, reportPath, proDir
        # 获取配置文件的目录
        proDir = readConfig.proDir
        reportPath = os.path.join(proDir, "reslut")
        # 如果日志目录不存在，则创建目录
        if not os.path.exists(reportPath):
            os.mkdir(reportPath)
        # 日志文件夹保存的格式
        logPath = os.path.join(reportPath, str(datetime.now().strftime("%Y%m%d")))
        if not os.path.exists(logPath):
            os.mkdir(logPath)
        # 打印日志
        self.logger = logging.getLogger(__name__)
        # 设定日志级别
        self.logger.setLevel(level = logging.INFO)
        # 日志输出文件
        self.handler = logging.FileHandler(os.path.join(os.path.join(logPath, "output.log")))
        self.handler.setLevel(logging.INFO)
        self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        self.handler.setFormatter(self.formatter)
        # 设定控制台日志输出级别
        self.console = logging.StreamHandler()
        self.console.setLevel(logging.INFO)
        # 添加日知道控制台和文件
        self.logger.addHandler(self.handler)
        self.logger.addHandler(self.console)

    def get_log(self):
        return self.logger


class MyLog:
    log = None
    mutex = threading.Lock()
    def __init__(self):
        pass
    # 启用线程添加日志
    @staticmethod
    def get_log():
        if MyLog.log is None:
            MyLog.mutex.acquire()
            MyLog.log = Log()
            MyLog.mutex.release()

        return MyLog.log

# MyLog.log = Log()
# log = MyLog.get_log()
# logger = log.logger
# logger.info('666')